﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XMK.Framework.Model;
using XMK.Tadong.Data;
using XMK.Tadong.Model;
using XMK.Utils;
using XMK.Tadong.Model.Cond;
using System.Data;
using System.Configuration;
using System.IO;
namespace XMK.Tadong.Biz
{
    /// <summary>
    /// 个人空间文档信息
    /// </summary>
    public class TB_PERSON_FILEBiz
    {
        public TB_PERSON_FILEBiz()
        {
        }

        public bool Upload(TB_PERSON_FILEInfo fileObj)
        {
            bool result = false;

            Tb_Person_FileData fileData = new Tb_Person_FileData(Config.TadongDbProvider);

            if (!fileData.IsExist(fileObj))
            {
                result = fileData.InsertNewFile(fileObj);
            }

            return result;
        }

        /// <summary>
        /// 判断同一用户，文件夹下是否已有同名文件
        /// </summary>
        /// <param name="?"></param>
        /// <returns></returns>
        public bool HasFileName(string User_ID, string File_Name, string Folder_ID)
        {
            Tb_Person_FileData fileData = new Tb_Person_FileData(Config.TadongDbProvider);

            return fileData.HasFileName(User_ID, File_Name, Folder_ID);
        }

        /// <summary>
        /// 判断同一目录下是否有多个引用
        /// </summary>
        /// <param name="?"></param>
        /// <returns></returns>
        public bool HasSavePath(string Save_Path)
        {
            Tb_Person_FileData fileData = new Tb_Person_FileData(Config.TadongDbProvider);

            return fileData.HasSavePath(Save_Path);
        }

        public bool CopyFile(TB_PERSON_FILEInfo fileObj)
        {
            bool result = false;

            Tb_Person_FileData fileData = new Tb_Person_FileData(Config.TadongDbProvider);

            result = fileData.InsertNewFile(fileObj);

            return result;
        }
        public bool Update(TB_PERSON_FILEInfo fileObj)
        {
            bool result = true;
            Tb_Person_FileData fileData = new Tb_Person_FileData(Config.TadongDbProvider);
            result = fileData.Update(fileObj);
            return result;
        }
        public TB_PERSON_FILEInfo GetFileInfoById(string fileId)
        {
            Tb_Person_FileData fileData = new Tb_Person_FileData(Config.TadongDbProvider);
            return fileData.GetFileInfoById(Convert.ToString(fileId));
        }
        public TB_PERSON_FILEInfo GetFileInfoBy_PathAndName(string path, string name, string userId)
        {
            Tb_Person_FileData fileData = new Tb_Person_FileData(Config.TadongDbProvider);
            return fileData.GetFileInfoBy_PathAndName(path, name, userId);
        }



        /// <summary>
        /// 删除数据库记录、删除共享文件记录.删除pdf swf
        /// </summary>
        /// <param name="path"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool DeleteBy_PathAndName(string path, string name, string userId)
        {

            Tb_Person_FileData fileData = new Tb_Person_FileData(Config.TadongDbProvider);
            Tb_Person_File_ShareData fileShareData = new Tb_Person_File_ShareData(Config.TadongDbProvider);
            TB_PERSON_FILEInfo fileInfo = GetFileInfoBy_PathAndName(path, name, userId);
            if (fileData.DeleteBy_PathAndName(path, name, userId))
            {
                if (!String.IsNullOrEmpty(fileInfo.PDF_PATH))
                {
                    string strPdfPath = ConfigurationManager.AppSettings["DiskPath"] + fileInfo.PDF_PATH;
                    if (File.Exists(strPdfPath))
                    {
                        File.Delete(strPdfPath);
                    }
                }

                if (!String.IsNullOrEmpty(fileInfo.SWF_PATH))
                {
                    string strSwfPath = ConfigurationManager.AppSettings["DiskPath"] + fileInfo.SWF_PATH;
                    if (File.Exists(strSwfPath))
                    {
                        File.Delete(strSwfPath);
                    }
                }

                return fileShareData.deleteShareFileInfoByFileNameAndPath(name, path);
            }
            return false;
        }



        /// <summary>
        /// 修改源数据
        /// </summary>
        /// <param name="model">实体对象</param>
        /// <returns>成功返回true,失败返回false</returns>
        public bool UpDateSourceData(TB_PERSON_FILEInfo model)
        {
            Tb_Person_FileData dal = new Tb_Person_FileData(Config.TadongDbProvider);
            return dal.UpDateSourceDataBySql(model);
        }


        /// <summary>
        /// 修改关键词和摘要信息
        /// </summary>
        /// <param name="model">实体对象</param>
        /// <returns>成功返回true,失败返回false</returns>
        public bool UpDateInfo(TB_PERSON_FILEInfo model, string type)
        {
            Tb_Person_FileData dal = new Tb_Person_FileData(Config.TadongDbProvider);
            return dal.UpDateInfoBySql(model, type);
        }


        /// <summary>
        /// 获取文件列表
        /// </summary>
        /// <param name="DestObjId">文件所在目标ID</param>
        /// <returns>文件列表</returns>
        public int GetCountByDestObj(string folder_id)
        {
            Tb_Person_FileData dal = new Tb_Person_FileData(Config.TadongDbProvider);
            return dal.GetCountByDestObj(folder_id);
        }

        /// <summary>
       /// 获取文件总数
       /// </summary>
       /// <param name="folder_id">文件夹ID</param>
       /// <param name="keyword">关键词</param>
       /// <returns></returns>
        public int GetCountByDestObjByKeyWord(string folder_id, string keyword)
        {
            Tb_Person_FileData dal = new Tb_Person_FileData(Config.TadongDbProvider);
            return dal.GetCountByDestObjByKeyWord(folder_id, keyword);
        }

        /// <summary>
        /// 获取文件列表
        /// </summary>
        /// <param name="DestObjId">文件所在目标ID</param>
        /// <returns>文件列表</returns>
        public DataTable GetModelByDestObjAndType(string folder_id, string Type, int startNum, int endNum, string Person_ID, ref int total)
        {
            Tb_Person_FileData dal = new Tb_Person_FileData(Config.TadongDbProvider);
            return dal.GetModelByDestObjAndType(folder_id, Type, startNum, endNum, Person_ID, ref total);
        }

         /// <summary>
      /// 搜索文件列表
      /// </summary>
      /// <param name="folder_id">文件夹ID</param>
      /// <param name="startNum"></param>
      /// <param name="endNum"></param>
      /// <param name="ID">用户ID</param>
      /// <param name="keyword">关键词</param>
      /// <param name="total"></param>
      /// <returns></returns>
        public DataTable GetModelByDestObjAndKeyWord(string folder_id, int startNum, int endNum, string ID, string keyword, ref int total)
        {
            Tb_Person_FileData dal = new Tb_Person_FileData(Config.TadongDbProvider);
            return dal.GetModelByDestObjAndKeyWord(folder_id, startNum, endNum, ID, keyword, ref total);
        }

        /// <summary>
        /// 获取文件列表
        /// </summary>
        /// <param name="DestObjId">文件所在目标ID</param>
        /// <returns>文件列表</returns>
        public DataTable GetModelByDestObj(string folder_id, int startNum, int endNum, string Person_ID, ref int total)
        {
            Tb_Person_FileData dal = new Tb_Person_FileData(Config.TadongDbProvider);
            return dal.GetModelByDestObj(folder_id, startNum, endNum, Person_ID, ref total);
        }

        /// <summary>
        /// 获取文件列表
        /// </summary>
        /// <param name="DestObjId">文件所在目标ID</param>
        /// <returns>文件列表</returns>
        public DataTable GetPersonFile(string folder_id)
        {
            Tb_Person_FileData dal = new Tb_Person_FileData(Config.TadongDbProvider);
            return dal.GetPersonFileByDestObj(folder_id);
        }

        /// <summary>
        /// 获取文件列表
        /// </summary>
        /// <param name="DestObjId">文件所在目标ID</param>
        /// <returns>文件列表</returns>
        public DataTable GeTeamFile(string folder_id)
        {
            Tb_Person_FileData dal = new Tb_Person_FileData(Config.TadongDbProvider);
            return dal.GetTeamFileByDestObj(folder_id);
        }

        //数据库是否有文件
        public bool IsMoreThanOne(string File_ID)
        {
            Tb_Person_FileData dal = new Tb_Person_FileData(Config.TadongDbProvider);
            return dal.IsMoreThanOne(File_ID);

        }

        //根据文件ID删除记录
        public bool Delete(string File_ID)
        {
            Tb_Person_FileData dal = new Tb_Person_FileData(Config.TadongDbProvider);
            return dal.DeleteBySql(File_ID);

        }
        /// <summary>
        /// 获取回收站信息
        /// </summary>
        /// <param name="strWhere">条件</param>
        /// <returns>结果集</returns>
        public DataTable GetRecycleDataBySql(string User_ID, string sorttype, string sortdesc, int startNum, int endNum, ref int total)
        {
            Tb_Person_FileData dal = new Tb_Person_FileData(Config.TadongDbProvider);
            return dal.GetRecycleDataBySql(User_ID, sorttype, sortdesc,startNum, endNum, ref total);
        }

        //根据文件ID将文件移动到回收站
        public bool RemoveToRecycle(string File_ID)
        {
            Tb_Person_FileData dal = new Tb_Person_FileData(Config.TadongDbProvider);
            return dal.RemoveToRecycleBySql(File_ID);
        }

        /// <summary>
        /// 获取文件列表
        /// </summary>
        /// <param name="FileID">文件IDs</param>
        /// <returns>文件列表</returns>
        public DataTable GetModelList(string FileID)
        {
            Tb_Person_FileData dal = new Tb_Person_FileData(Config.TadongDbProvider);
            return dal.GetModelListBySql(FileID);
        }

        public TB_PERSON_FILEInfo GetModelLists(string FileID)
        {
            Tb_Person_FileData dal = new Tb_Person_FileData(Config.TadongDbProvider);
            return dal.GetModelListsBySql(FileID);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="fileId">ids</param>
        /// <returns></returns>
        public bool DeleteIN(string fileId)
        {
            Tb_Person_FileData dal = new Tb_Person_FileData(Config.TadongDbProvider);
            return dal.DeleteIN(fileId);
        }

        //根据文件ID将文件从回收站移回空间
        public bool RecycleToSpace(string File_ID)
        {
            Tb_Person_FileData dal = new Tb_Person_FileData(Config.TadongDbProvider);
            return dal.RecycleToSpaceBySql(File_ID);
        }
        //根据文件名获取当前空间同名文件的最大版本号
        public decimal GetMaxFileVersion(string fileName, string UserID)
        {
            Tb_Person_FileData dal = new Tb_Person_FileData(Config.TadongDbProvider);
            return dal.GetMaxFileVersion(fileName, UserID);
            
        }
        /// <summary>
        /// 分类查看查询数据
        /// </summary>
        /// <param name="teamId"></param>
        /// <returns></returns>
        public DataTable getXmkClassFlie(string personId,string FOLDER_ID, string classType)
        {
            Tb_Person_FileData dal = new Tb_Person_FileData(Config.TadongDbProvider);
            return dal.getXmkClassFlie(personId,FOLDER_ID, classType);
        }
        /// <summary>
        /// 分类查看明细数据
        /// </summary>
        /// <param name="teamId"></param>
        /// <returns></returns>
        public DataTable getXmkClassDetailFile(string personId,string sqlwhere)
        {
            Tb_Person_FileData dal = new Tb_Person_FileData(Config.TadongDbProvider);
            return dal.getXmkClassDetailFile(personId, sqlwhere);
        }
        public XMKFileExtInfo GetModelByFileId(string fileId)
        {
            Tb_Person_FileData dal = new Tb_Person_FileData(Config.TadongDbProvider);
            return dal.GetModelByFileId(fileId);
        }
    }
}
